package com.team2.cyber_eye.background_task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import com.team2.cyber_eye.R;
import com.team2.cyber_eye.ActivityHelpers.Util;
import com.team2.shared.CyberServer;
import com.team2.shared.Product;
import com.team2.shared.Store;
import com.team2.shoppingService.Server;

import android.app.Activity;
import android.os.AsyncTask;

public class RetrieveStoresTask extends AsyncTask<Object, Integer, ArrayList<Store>> {
	
	public interface LoadingTaskFinishedListener {
		void onTaskFinished(ArrayList<Store> stores); // If you want to pass something back to the listener add a param to this method
	}
	
	// This is the progress bar you want to update while the task is in progress
	// This is the listener that will be told when this task is finished
	private final LoadingTaskFinishedListener finishedListener;
	private final Activity creator;
	
	/**
	 * A Loading task that will load some resources that are necessary for the app to start
	 * @param progressBar - the progress bar you want to update while the task is in progress
	 * @param finishedListener - the listener that will be told when this task is finished
	 */
	public RetrieveStoresTask(LoadingTaskFinishedListener finishedListener, Activity creator)
	{
		this.finishedListener = finishedListener;
		this.creator = creator;
	}

	@Override
	protected ArrayList<Store> doInBackground(Object... params) {
		ArrayList<Store> stores;
		CyberServer server = new Server();
		
		Product product = (Product) params[0];
		double latitude = Double.parseDouble(params[1].toString());
		double longitude = Double.parseDouble(params[2].toString());
		int radius = (Integer) params[3];
		
		stores = server.searchStores(product, latitude, longitude, radius);
		
		Collections.sort(stores, new Comparator<Store>() {
		    @Override
		    public int compare(Store s1, Store s2) {
		        return s1.getPrice().compareTo(s2.getPrice());
		    }
		});
		
		ArrayList<Store> tempStores = new ArrayList<Store>();
		
		for(Store store : stores){
			tempStores.add(store);
		}
			
		for (Store store : tempStores) {
			if(!isValidStoreName(store.getName())){
				stores.remove(store);
			}
		}
		
		return stores;
	}
	
	private boolean isValidStoreName(String name){
		boolean isValid = false;
		
		String storeName = name.toLowerCase();
		try{
			if(storeName.contains("best buy")) {
	        	isValid = true; 
	        } else if(storeName.contains("future")) {
	        	isValid = true;
	        } else if(storeName.contains("chapters")) {
	        	isValid = true;
	        } else if(storeName.contains("sears")) {
	        	isValid = true;
	        } else if(storeName.contains("bay")) {
	        	 isValid = true;
	        } else if(storeName.contains("walmart")) {
	        	isValid = true;
	        } else if(storeName.contains("london")) {
	        	isValid = true;
	        } else if(storeName.contains("auto")){
	        	isValid = false;
	        }
		}catch(Exception e){
			Util.LogError(e);
		}
		
		
		return isValid;
	}
	
	@Override
	protected void onPostExecute(ArrayList<Store> result) {
		super.onPostExecute(result);
		finishedListener.onTaskFinished(result); // Tell whoever was listening we have finished
	}

}
